package com.hmh.spring.boot.blog.domain;

import com.hmh.spring.boot.blog.domain.Blog;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldIndex;
import java.io.Serializable;
import java.sql.Timestamp;

/**
 * ES中博客文档实体
 * indexName：索引库的名称
 *
 * @author hmh
 * @date 2019/3/12
 */
@Document(indexName = "blog", type = "blog")
public class EsBlog implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 主键
     */
    @Id
    private String id;

    /**
     * Blog 的 id（不分词索引）
     */
    @Field(index = FieldIndex.not_analyzed)
    private Long blogId;

    /**
     * 标题（分词索引）
     */
    private String title;

    /**
     * 摘要（分词索引）
     */
    private String summary;

    /**
     * 正文（分词索引）
     */
    private String content;

    /**
     * 用户名（不做全文检索字段）
     */
    @Field(index = FieldIndex.not_analyzed)
    private String username;

    /**
     * 用户头像（不做全文检索字段）
     */
    @Field(index = FieldIndex.not_analyzed)
    private String avatar;

    /**
     * 创建时间（不做全文检索字段）
     */
    @Field(index = FieldIndex.not_analyzed)
    private Timestamp createTime;

    /**
     * 访问量、阅读量（不做全文检索字段）
     */
    @Field(index = FieldIndex.not_analyzed)
    private Long readSize = 0L;

    /**
     * 评论量（不做全文检索字段）
     */
    @Field(index = FieldIndex.not_analyzed)
    private Long commentSize = 0L;

    /**
     * 点赞量（不做全文检索字段）
     */
    @Field(index = FieldIndex.not_analyzed)
    private Long voteSize = 0L;

    /**
     * 标签（分词索引）
     */
    private String tags;

    /**
     * JPA 的规范要求无参构造函数；设为 protected 防止直接使用
     *
     * @author hmh
     * @date 2019/3/12
     * @param
     * @return
     */
    protected EsBlog() {
    }

    public EsBlog(String title, String content) {
        this.title = title;
        this.content = content;
    }

    public EsBlog(Long blogId, String title, String summary, String content, String username, String avatar,Timestamp createTime,
                  Long readSize,Long commentSize, Long voteSize , String tags) {
        this.blogId = blogId;
        this.title = title;
        this.summary = summary;
        this.content = content;
        this.username = username;
        this.avatar = avatar;
        this.createTime = createTime;
        this.readSize = readSize;
        this.commentSize = commentSize;
        this.voteSize = voteSize;
        this.tags = tags;
    }

    /**
     * 构造方法（便于普通实体转成ES实体）
     *
     * @author hmh
     * @date 2019/3/12
     * @param blog
     * @return
     */
    public EsBlog(Blog blog){
        this.blogId = blog.getId();
        this.title = blog.getTitle();
        this.summary = blog.getSummary();
        this.content = blog.getContent();
        this.username = blog.getUser().getUsername();
        this.avatar = blog.getUser().getAvatar();
        this.createTime = blog.getCreateTime();
        this.readSize = blog.getReadSize();
        this.commentSize = blog.getCommentSize();
        this.voteSize = blog.getVoteSize();
        this.tags = blog.getTags();
    }

    public void update(Blog blog){
        this.blogId = blog.getId();
        this.title = blog.getTitle();
        this.summary = blog.getSummary();
        this.content = blog.getContent();
        this.username = blog.getUser().getUsername();
        this.avatar = blog.getUser().getAvatar();
        this.createTime = blog.getCreateTime();
        this.readSize = blog.getReadSize();
        this.commentSize = blog.getCommentSize();
        this.voteSize = blog.getVoteSize();
        this.tags = blog.getTags();
    }


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
    public Long getBlogId() {
        return blogId;
    }

    public void setBlogId(Long blogId) {
        this.blogId = blogId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public Timestamp getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Timestamp createTime) {
        this.createTime = createTime;
    }

    public Long getReadSize() {
        return readSize;
    }

    public void setReadSize(Long readSize) {
        this.readSize = readSize;
    }

    public Long getCommentSize() {
        return commentSize;
    }

    public void setCommentSize(Long commentSize) {
        this.commentSize = commentSize;
    }

    public Long getVoteSize() {
        return voteSize;
    }

    public void setVoteSize(Long voteSize) {
        this.voteSize = voteSize;
    }

    public String getTags() {
        return tags;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public void setTags(String tags) {
        this.tags = tags;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public String toString() {
        return String.format(
                "User[id=%d, title='%s', content='%s']",
                blogId, title, content);
    }

}